import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import _useMediaQuery from "@mui/material/useMediaQuery";
import React, { useRef } from "react";
import BScroll from "@better-scroll/core";
import PullDown from "@better-scroll/pull-down";
import MouseWheel from "@better-scroll/mouse-wheel";
import { useMount, useUnmount } from "ahooks";
import styles from "./index.module.less";
import { useControllableValue, useUpdateEffect } from "ahooks";
import classNames from "classnames";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
BScroll.use(PullDown);
BScroll.use(MouseWheel);
var TIME_BOUNCE = 800;
var THRESHOLD = 70;
var STOP = 56;
var PullRefresh = function(props) {
    var initBscroll = function initBscroll() {
        if (bsWrapper.current) {
            bscroll.current = new BScroll(bsWrapper.current, {
                scrollY: true,
                bounceTime: TIME_BOUNCE,
                useTransition: isPc ? true : false,
                click: true,
                disableMouse: false,
                pullDownRefresh: {
                    threshold: THRESHOLD,
                    stop: STOP
                },
                mouseWheel: {
                    speed: 100
                }
            });
        }
    };
    var initEvent = function initEvent() {
        var _bscroll_current, _bscroll_current1, _bscroll_current2;
        (_bscroll_current = bscroll.current) === null || _bscroll_current === void 0 ? void 0 : _bscroll_current.on("pullingDown", pullingDownHandler);
        (_bscroll_current1 = bscroll.current) === null || _bscroll_current1 === void 0 ? void 0 : _bscroll_current1.on("enterThreshold", enterThresholdHandler);
        (_bscroll_current2 = bscroll.current) === null || _bscroll_current2 === void 0 ? void 0 : _bscroll_current2.on("leaveThreshold", leaveThresholdHandler);
    };
    var cancelEvent = function cancelEvent() {
        var _bscroll_current, _bscroll_current1, _bscroll_current2;
        (_bscroll_current = bscroll.current) === null || _bscroll_current === void 0 ? void 0 : _bscroll_current.off("pullingDown", pullingDownHandler);
        (_bscroll_current1 = bscroll.current) === null || _bscroll_current1 === void 0 ? void 0 : _bscroll_current1.off("enterThreshold", enterThresholdHandler);
        (_bscroll_current2 = bscroll.current) === null || _bscroll_current2 === void 0 ? void 0 : _bscroll_current2.off("leaveThreshold", leaveThresholdHandler);
    };
    var children = props.children, onRefresh = props.onRefresh, loadingText = props.loadingText, loosingText = props.loosingText, propsLoading = props.loading, onLoadingChange = props.onLoadingChange, pullingText = props.pullingText, successText = props.successText, failText = props.failText, wrapClassName = props.wrapClassName, className = props.className, dependencies = props.dependencies;
    var isPc = _useMediaQuery("(min-width:1280px)");
    var bscroll = React.useRef(null);
    var _useControllableValue = _sliced_to_array(useControllableValue({
        value: propsLoading,
        onChange: onLoadingChange
    }), 2), setLoading = _useControllableValue[1];
    var _React_useState = _sliced_to_array(React.useState(""), 2), tip = _React_useState[0], setTip = _React_useState[1];
    var bsWrapper = useRef(null);
    useMount(function() {
        initBscroll();
        initEvent();
    });
    useUnmount(function() {
        cancelEvent();
    });
    useUpdateEffect(function() {
        var _bscroll_current;
        (_bscroll_current = bscroll.current) === null || _bscroll_current === void 0 ? void 0 : _bscroll_current.refresh();
    }, dependencies);
    function pullingDownHandler() {
        return _pullingDownHandler.apply(this, arguments);
    }
    function _pullingDownHandler() {
        _pullingDownHandler = _async_to_generator(function() {
            var _bscroll_current, e;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        _state.trys.push([
                            0,
                            2,
                            ,
                            3
                        ]);
                        setLoading(true);
                        setTip(loadingText);
                        return [
                            4,
                            onRefresh === null || onRefresh === void 0 ? void 0 : onRefresh()
                        ];
                    case 1:
                        _state.sent();
                        setLoading(false);
                        (_bscroll_current = bscroll.current) === null || _bscroll_current === void 0 ? void 0 : _bscroll_current.finishPullDown();
                        setTip(successText);
                        setTimeout(function() {
                            var _bscroll_current;
                            (_bscroll_current = bscroll.current) === null || _bscroll_current === void 0 ? void 0 : _bscroll_current.refresh();
                        }, TIME_BOUNCE + 50);
                        return [
                            3,
                            3
                        ];
                    case 2:
                        e = _state.sent();
                        setLoading(false);
                        setTip(failText);
                        return [
                            3,
                            3
                        ];
                    case 3:
                        return [
                            2
                        ];
                }
            });
        });
        return _pullingDownHandler.apply(this, arguments);
    }
    var enterThresholdHandler = function() {
        setTip(pullingText);
    };
    var leaveThresholdHandler = function() {
        setTip(loosingText);
    };
    return /*#__PURE__*/ _jsx("div", {
        className: classNames(styles.pulldown_wrapper, wrapClassName),
        ref: bsWrapper,
        children: /*#__PURE__*/ _jsxs("div", {
            className: classNames(styles.pulldown_scroller, className),
            children: [
                /*#__PURE__*/ _jsx("div", {
                    className: styles.pulldown_tip,
                    children: tip
                }),
                children
            ]
        })
    });
};
export default PullRefresh;

 //# sourceMappingURL=index.js.map